# Maintainer: Wu Zhenyu <wuzy01@qq.com>

_realname=fzf
pkgbase="mingw-w64-${_realname}"
pkgname="$MINGW_PACKAGE_PREFIX-${_realname}"
pkgver=0.62.0
pkgrel=1
pkgdesc='Command-line fuzzy finder (mingw-w64)'
arch=('any')
mingw_arch=(mingw64 ucrt64 clang64 clangarm64)
msys2_references=(
  'archlinux: fzf'
)
url='https://github.com/junegunn/fzf'
license=('spdx:MIT')
makedepends=(
  "$MINGW_PACKAGE_PREFIX-go"
  "$MINGW_PACKAGE_PREFIX-cc"
)
options=('!strip')
source=("https://github.com/junegunn/fzf/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-detect-arch.patch")
sha256sums=('e5beae86a3d026b2c2cfc165715d45b831b9f337a9e96f711ba3bc3d15e50900'
            '91c5a10e23125bdfb8ccf03daabf65f984b7d203d1da40f9e749ee83ea6e7aca')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "$srcdir/${_realname}-${pkgver}"
  sed -i 's/-w /-w -linkmode external /' Makefile

  apply_patch_with_msg \
    001-detect-arch.patch
}

build() {
  cd "$srcdir/${_realname}-${pkgver}"
  export CGO_LDFLAGS="${LDFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export GOFLAGS="-trimpath -mod=readonly -modcacherw -buildvcs=false -buildmode=pie"

  FZF_VERSION=${pkgver} FZF_REVISION=tarball make
}

package() {
  cd "$srcdir/${_realname}-${pkgver}"

  ## Man page
  install -Dm644 man/man1/fzf.1 "$pkgdir"$MINGW_PREFIX/share/man/man1/fzf.1
  install -Dm644 man/man1/fzf-tmux.1 "$pkgdir"$MINGW_PREFIX/share/man/man1/fzf-tmux.1

  ## License
  install -Dm644 LICENSE "$pkgdir"$MINGW_PREFIX/share/licenses/fzf/LICENSE

  ## Binaries
  install -dm755 "$pkgdir"$MINGW_PREFIX/bin
  install -Dm755 target/fzf-* "$pkgdir"$MINGW_PREFIX/bin/fzf.exe

  # Shell scripts
  install -m755 bin/fzf-tmux "$pkgdir"$MINGW_PREFIX/bin/

  ## Completion and keybindings
  install -dm755 "$pkgdir"$MINGW_PREFIX/share/fzf
  install -m644 shell/*.bash shell/*.zsh "$pkgdir"$MINGW_PREFIX/share/fzf

  ## Fish keybindings
  install -Dm644 shell/key-bindings.fish "$pkgdir"$MINGW_PREFIX/share/fish/vendor_functions.d/fzf_key_bindings.fish

  ## Vim plugin
  install -Dm644 doc/fzf.txt "$pkgdir"$MINGW_PREFIX/share/vim/vimfiles/doc/fzf.txt
  install -Dm644 plugin/fzf.vim "$pkgdir"$MINGW_PREFIX/share/vim/vimfiles/plugin/fzf.vim
}
